package com.by.common.crud;

import com.by.common.bean.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public abstract class BaseServiceImpl<T,M extends IMapper> implements IService<T>{

    @Autowired
    protected M baseMapper;

    @Override
    public int saveEntity(T entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int updateEntity(T entity) {
        return baseMapper.updateById(entity);
    }

    @Override
    public int deleteEntity(Serializable id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public T getEntity(Serializable id) {
        return (T)baseMapper.selectById(id);
    }

    @Override
    public PageBean query(Map map) {
        int page=1;//默认查询第一页
        int pageSize=10;//默认每页记录数
        if(null!=map){
            //判断有没有传page
            if(map.containsKey("page")){
                page=Integer.parseInt(map.get("page").toString());
            }
            if(map.containsKey("pageSize")){
                pageSize=Integer.parseInt(map.get("pageSize").toString());
            }
        }
        PageHelper.startPage(page,pageSize);
        List list=baseMapper.query(map);
        PageInfo info=new PageInfo(list);
        return new PageBean(page,pageSize,info.getTotal(),list);
    }
}
